package com.olddriver.crm.web.controller;

import com.olddriver.crm.annotation.PermissionName;
import com.olddriver.crm.domain.Role;
import com.olddriver.crm.page.PageResult;
import com.olddriver.crm.query.QueryObject;
import com.olddriver.crm.service.IRoleService;
import com.olddriver.crm.util.JsonResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("role")
public class RoleController {
    @Autowired
    private IRoleService roleService;

    @RequiresPermissions("role:list")
    @PermissionName("查看角色")
    @RequestMapping("list")
    public String list() {
        return "role/list";
    }

    @ResponseBody
    @RequestMapping("query")
    @RequiresPermissions("role:query")
    @PermissionName("查询角色")
    public PageResult query(QueryObject qo) {
        PageResult result = roleService.query(qo);
        return result;
    }

    @ResponseBody
    @RequestMapping("saveOrUpdate")
    @RequiresPermissions("role:saveOrUpdate")
    @PermissionName("编辑角色")
    public JsonResult saveOrUpdate(Role role) {
        Long id = role.getId();
        try {
            if (id != null) {
                roleService.update(role);
            } else {
                roleService.insert(role);
            }
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.faliure();
        }
    }

    @ResponseBody
    @RequestMapping("delete")
    @RequiresPermissions("role:delete")
    @PermissionName("删除角色")
    public JsonResult delete(Long id) {
        try {
            roleService.delete(id);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.faliure();
        }
    }
    @ResponseBody
    @RequestMapping("listAll")
    public List<Role> listAll(Long id) {
        return roleService.listAll();
    }
}
